package edu.whu.irlab.woscrawl.pubmed;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by zhongwei on 2017/3/3.
 */
public class Search {

    WebDriver driver;
    Set<String> done;

    static void pagination(WebDriver driver, FileWriter fileWriter) throws IOException {
        while (true) {
            List<WebElement> results = driver.findElements(By.className("search-results-item"));
            for (WebElement result : results) {
                tryPro(result, fileWriter);
            }
            WebElement next = driver.findElement(By.className("paginationNext"));
            if (next == null)
                break;
            if (next.getAttribute("title").equals("下一页(不活动)"))
                break;
            next.click();
        }
    }

    static void tryPro(WebElement result, FileWriter fileWriter) {
        try {
            WebElement content = result.findElement(By.className("search-results-content"));
            WebElement data = result.findElement(By.className("search-results-data"));
            WebElement smallV110 = content.findElement(By.className("smallV110"));
            String link = smallV110.getAttribute("href");
            String title = smallV110.getText();
            WebElement citeEle = data.findElement(By.className("search-results-data-cite")).findElement(By.tagName("a"));
            String citeLink = citeEle.getAttribute("href");
            fileWriter.write(link + "\n");
            fileWriter.write(title + "\n");
            fileWriter.write(citeLink + "\n\n\n");
        } catch (Exception ex) {
            tryPro(result, fileWriter);
        }
    }

    public static void main(String[] args) throws IOException {
//        WebDriver driver = new ChromeDriver();
//        driver.get("http://apps.webofknowledge.com/summary.do?product=WOS&parentProduct=WOS&search_mode=AdvancedSearch&qid=48&SID=4EjuwNZXzYJX7ORttDN&page=1&action=changePageSize&pageSize=50");
//        FileWriter fileWriter = new FileWriter("LinksToDo.txt");
//        pagination(driver, fileWriter);
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for (int i = 45; i <= 45; i++) {
            executorService.execute(new Worker(new FileWriter("linksToDo_" + i + ".txt"), "http://apps.webofknowledge.com/summary.do?product=WOS&parentProduct=WOS&search_mode=AdvancedSearch&qid=48&SID=4EjuwNZXzYJX7ORttDN&page=" + i + "&action=changePageSize&pageSize=50"));
        }
        executorService.shutdown();
    }

    public static void search(WebDriver driver) throws IOException {
        String searchExpression = FileUtils.readFileToString(new File("search.txt"));
        driver.get("http://www.lib.whu.edu.cn/web/dzzy/detail.asp?q=IDN=WHU05737&s=detail&full=Y");
        driver.findElement(By.id("url")).click();
        WebElement options = driver.findElement(By.className("icon-dd-active-block-search"));
        options.click();
        List<WebElement> list = options.findElements(By.tagName("li"));
        list.get(list.size() - 1).click();
        driver.findElement(By.className("Adv_formBoxesSearch")).sendKeys(searchExpression);
        driver.findElement(By.id("searchButton")).click();
        List<WebElement> histories = driver.findElements(By.className("historyResults"));
        histories.get(0).click();
    }

    void onePage() {

    }

    static class Worker extends Thread {
        private WebDriver driver;
        private FileWriter fileWriter;
        private String url;

        public Worker(FileWriter fileWriter, String url) {
            this.fileWriter = fileWriter;
            this.url = url;
        }

        @Override
        public void run() {
            driver = new ChromeDriver();
            driver.get(url);
            List<WebElement> results = driver.findElements(By.className("search-results-item"));
            for (WebElement result : results) {
                tryPro(result, fileWriter);
            }
            try {
                fileWriter.flush();
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            driver.quit();
        }
    }
}
